package com.whsxt.sys.controller;


import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.whsxt.common.CodeMsg;
import com.whsxt.common.PageInfo;
import com.whsxt.common.Result;
import com.whsxt.sys.data.LogininfoData;
import com.whsxt.sys.domain.Logininfo;
import com.whsxt.sys.service.ILogininfoService;
import cn.hutool.core.util.StrUtil;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author 贺炯
 * @since 2019-09-21
 */
@RestController
@RequestMapping("/sys/logininfo")
public class LogininfoController {
	
	@Autowired
	private ILogininfoService iLogininfoService;
	
	/**
	 * 列表展示
	 * @param logininfoData
	 * @return
	 */
	@RequestMapping("/list")
	public Object list(LogininfoData logininfoData) {
		QueryWrapper<Logininfo> queryWrapper = new QueryWrapper<Logininfo>();
		queryWrapper.like(StrUtil.isNotBlank(logininfoData.getLoginname()), "loginname", logininfoData.getLoginname());
		queryWrapper.eq(StrUtil.isNotBlank(logininfoData.getLoginip()), "loginip", logininfoData.getLoginip());
		queryWrapper.gt(StrUtil.isNotBlank(logininfoData.getStartTime()), "logintime",logininfoData.getStartTime());
		queryWrapper.lt(StrUtil.isNotBlank(logininfoData.getEndTime()), "logintime", logininfoData.getEndTime());
		queryWrapper.orderByDesc("logintime");
		Page<Logininfo> page = new Page<Logininfo>(logininfoData.getPage(), logininfoData.getLimit()); 
		iLogininfoService.page(page , queryWrapper);
		PageInfo pageInfo = new PageInfo(page.getTotal(), page.getRecords());
		return new Result(pageInfo);
	}
	/**
	 * 批量删除
	 */
	@RequestMapping("/batchDelete")
	public Object batchDelete(Integer[] ids) {
		Collection<Serializable> idList = new ArrayList<Serializable>();
		for (Integer id : ids) {
			idList.add(id);
		}
		try {
			iLogininfoService.removeByIds(idList);
			return new Result(CodeMsg.LOGININFO_BATCH_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.LOGININFO_BATCH_DELETE_ERROR);
		}
	}
	/**
	 * 删除一个
	 * @param id
	 * @return
	 */
	@RequestMapping("/deleteOne")
	public Object deleteOne(Integer id) {
		try {
			iLogininfoService.removeById(id);
			return new Result(CodeMsg.LOGININFO_DELETE_SECCUSS);
		} catch (Exception e) {
			e.printStackTrace();
			return new Result(CodeMsg.LOGININFO_DELETE_ERROR);
		}	
	}
}

